package com.lovo.homework7_14ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModifyPanel extends JPanel implements ActionListener {
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;

    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;

    private JTextField  jTextField1;
    private JTextField  jTextField2;
    private JTextField  jTextField3;

    private Icon buttonIcon1 =util.Util.getIcon("/images/buttonSure.jpg");
    private Icon buttonIcon2 =util.Util.getIcon("/images/buttonClear.jpg");
    private Icon buttonIcon3 =util.Util.getIcon("/images/buttonOff.jpg");

    private ATMmain atMmain;

    private int count = 3;

    public ModifyPanel(ATMmain atMmain){
        this.atMmain=atMmain;
        this.setLayout(null);
        this.setOpaque(false);

        jLabel1=new JLabel("请输入原密码");
        jLabel1.setBounds(300,230,250,50);
        jLabel1.setForeground(Color.blue);
        jLabel1.setFont(new Font("宋体",Font.BOLD,20));
        this.add(jLabel1);
        jTextField1 = new JTextField("");
        jTextField1.setBounds(500,235,200,30);
        this.add(jTextField1);

        jLabel2=new JLabel("请输入新密码");
        jLabel2.setBounds(250,290,300,50);
        jLabel2.setForeground(Color.blue);
        jLabel2.setFont(new Font("宋体",Font.BOLD,20));
        this.add(jLabel2);
        jTextField2 = new JTextField("");
        jTextField2.setBounds(500,295,200,30);
        this.add(jTextField2);

        jLabel3=new JLabel("请再次输入，确认密码");
        jLabel3.setBounds(250,350,300,50);
        jLabel3.setForeground(Color.blue);
        jLabel3.setFont(new Font("宋体",Font.BOLD,20));
        this.add(jLabel3);
        jTextField3 = new JTextField("");
        jTextField3.setBounds(500,355,200,30);
        this.add(jTextField3);

        jButton1=new JButton(buttonIcon1);
        jButton1.setBounds(210,450,140,50);
        this.add(jButton1);
        jButton1.addActionListener(this);

        jButton2=new JButton(buttonIcon2);
        jButton2.setBounds(450,450,140,50);
        this.add(jButton2);
        jButton2.addActionListener(this);

        jButton3=new JButton(buttonIcon3);
        jButton3.setBounds(690,450,140,50);
        this.add(jButton3);
        jButton3.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       if (e.getSource()==jButton1){
           for (int i = 1; i >0 ; i--) {
               if (jTextField1.getText().equals(atMmain.getLoginUser().getUserPassWord())){
                   if (jTextField2.getText().equals(jTextField3.getText())){
                       JOptionPane.showMessageDialog(atMmain,"修改成功");
                       atMmain.getLoginUser().setUserPassWord(jTextField2.getText());
                       atMmain.switchPanel("menuPanel");
                   }else {
                       JOptionPane.showMessageDialog(atMmain,"两次输入的新密码不一致，请重新输入");
                       jTextField1.setText("");
                       jTextField2.setText("");
                       jTextField3.setText("");
                       i--;
                   }
               }else {
                   count--;
                   JOptionPane.showMessageDialog(atMmain,"原密码错误，请重新输入，你还有"+(count)+"次机会");
                   jTextField1.setText("");
                   jTextField2.setText("");
                   jTextField3.setText("");
                   i--;
               }
           }
           if (count==0){
               JOptionPane.showMessageDialog(atMmain,"次数用尽，卡被吞掉");
               System.exit(0);
           }
           atMmain.switchPanel("menuPanel");
       }else if (e.getSource()==jButton2){
           jTextField1.setText("");
           jTextField2.setText("");
           atMmain.switchPanel("modifyPanel");
       }else if (e.getSource()==jButton3){
           atMmain.switchPanel("loginPanel");
       }
    }
}
